package cn.xinfei.xdecision.data.core.util;

import cn.xinfei.xdecision.common.model.datax.datamanage.v2.VariableCallLog;
import cn.xinfei.xdecision.common.model.datax.enums.CollectStageEnum;
import cn.xinfei.xdecision.common.model.datax.enums.SourceType;
import cn.xinfei.xdecision.common.model.datax.enums.VariableEnum;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.collections4.MapUtils;

import java.util.*;
import java.util.stream.Collectors;

public class VariableCallLogUtils {


    public static VariableCallLog buildVarCallLog(String varCode,
                                                  Map<String, Object> inputParam,
                                                  String resultValue,
                                                  long dataSourceId,
                                                  VariableEnum variableEnum,
                                                  CollectStageEnum stage,
                                                  SourceType sourceType,
                                                  long start,
                                                  String requsetId) {

        long end = System.currentTimeMillis();
        VariableCallLog callLog = new VariableCallLog();
        callLog.setInputParam(JSONObject.toJSONString(inputParam));
        callLog.setDuration((end - start));
        callLog.setVarValue(resultValue);
        callLog.setVarCode(varCode);
        callLog.setSourceId(dataSourceId);
        callLog.setSourceType(sourceType.name());
        callLog.setVarType(Integer.valueOf(variableEnum.getCode()));
        callLog.setScene(stage.getCode());
        callLog.setRequestId(requsetId);
        return callLog;
    }

    public static List<VariableCallLog> buildVarCallLogsFromMap(Map<String, Object> executorResult,
                                                                Map<String, Object> inputParam,
                                                                long dataSourceId,
                                                                VariableEnum variableEnum,
                                                                CollectStageEnum stage,
                                                                SourceType sourceType,
                                                                long start,
                                                                String requsetId) {

        if (MapUtils.isEmpty(executorResult)) {
            return Collections.emptyList();
        }
        return executorResult.entrySet().stream()
                .map(entry -> {
                    List<VariableCallLog> varCallLogs = new ArrayList<>();
                    VariableCallLog variableCallLog = buildVarCallLog(entry.getKey(),
                            inputParam,
                            String.valueOf(entry.getValue()),
                            dataSourceId,
                            variableEnum,
                            stage,
                            sourceType,
                            start,
                            requsetId
                    );
                    varCallLogs.add(variableCallLog);
                    return varCallLogs;
                })
                .flatMap(Collection::stream)
                .collect(Collectors.toList());
    }
}
